package com.neusoft.view;

import com.neusoft.controller.ProductController;
import com.neusoft.model.Product;

import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;

public class View {

    private static final String CONTEXT = "欢迎来到商品管理系统：\n" +
            "下面是商品的功能列表：\n" +
            "[MAIN/M]:主菜单\n" +
            "[QUERY/Q]:查看全部商品的信息\n" +
            "[GET/G]:查看某商品的详细信息\n" +
            "[ADD/A]:添加商品信息\n" +
            "[UPDATE/U]:更新商品信息\n" +
            "[DELETE/D]:删除商品信息\n" +
            "[SEARCH/S]:查询商品信息(根据商品名来查询)\n" +
            "[EXIT/E]:退出商品管理系统\n" +
            "[BREAK/B]:退出当前功能，返回主菜单";

    private static final String OPERATION_MAIN = "MAIN";// 主菜单
    private static final String OPERATION_QUERY = "QUERY";// 查询
    private static final String OPERATION_GET = "GET";// 获取商品信息
    private static final String OPERATION_ADD = "ADD";// 添加
    private static final String OPERATION_UPDATE = "UPDATE";// 更新
    private static final String OPERATION_DELETE = "DELETE";// 删除
    private static final String OPERATION_SEARCH = "SEARCH";// 根据名称查询
    private static final String OPERATION_EXIT = "EXIT";// 退出
    private static final String OPERATION_BREAK = "BREAK";// 返回主菜单

    private static Scanner scan;


    public static void main(String[] args) {
        System.out.println(CONTEXT);
        ProductController action = new ProductController();
        Product product = new Product();
        String preOpt = null;
        Integer step = 1;
        // 保持程序一直运行
        scan = new Scanner(System.in);
        while (scan.hasNext()) {
            String in = scan.next();
            //主菜单
            if (OPERATION_MAIN.equals(in.toUpperCase())
                    || OPERATION_MAIN.substring(0, 1).equals(in.toUpperCase())) {
                System.out.println(CONTEXT);
                preOpt = null;
                step = 1;
            } else if (OPERATION_GET.equals(in.toUpperCase()) || OPERATION_GET.substring(0, 1).equals(in.toUpperCase())) {
                preOpt = OPERATION_GET;
                System.out.print("请输入商品【Id】:");
                Product p = action.getInfo(scan.nextInt());
                if (p != null) {
                    System.out.println("商品ID：" + p.getId() + " 类别：" + p.getType() + " 名称：" + p.getName() + " 价格：" + p.getPrice());
                } else {
                    System.out.println("该商品不存在!");
                }
            } else if (OPERATION_DELETE.equals(in.toUpperCase()) || OPERATION_DELETE.substring(0, 1).equals(in.toUpperCase())) {
                preOpt = OPERATION_DELETE;
                System.out.print("请输入要删除的商品【Id】:");
                int count = action.deleteProductById(scan.nextInt());
                if (count > 0) {
                    System.out.println("删除成功!");
                } else {
                    System.out.println("删除失败!");
                }
            } else if (OPERATION_QUERY.equals(in.toUpperCase()) || OPERATION_QUERY.substring(0, 1).equals(in.toUpperCase())) {
                preOpt = OPERATION_QUERY;
                List<Product> list = action.getProductList();
                for (Product p : list) {
                    System.out.println(p);
                }
            } else if (OPERATION_EXIT.equals(in.toUpperCase()) || OPERATION_EXIT.substring(0, 1).equals(in.toUpperCase())) {
                System.out.print("已退出商品管理系统！");
                break;
            }else if (OPERATION_SEARCH.equals(in.toUpperCase()) || OPERATION_SEARCH.substring(0, 1).equals(in.toUpperCase())) {
                preOpt = OPERATION_SEARCH;
                System.out.print("请输入要查询的商品名称：");
                String name = scan.next();
                List<Product> list = action.selectProductListByName(name);
                for (Product p : list) {
                    System.out.println(p);
                }
            }
            if (OPERATION_ADD.equals(in.toUpperCase()) || OPERATION_ADD.substring(0, 1).equals(in.toUpperCase())
                    || OPERATION_ADD.equals(preOpt)) {
                preOpt = OPERATION_ADD;
                // 新增商品
                if (1 == step) {
                    System.out.print("请输入商品【ID】:");
                } else if (2 == step) {
                    product.setId(Integer.valueOf(in));
                    System.out.print("请输入商品【名称】:");
                } else if (3 == step) {
                    product.setName(in);
                    System.out.print("请输入商品【类型】:");
                } else if (4 == step) {
                    product.setType(in);
                    System.out.print("请输入商品【价格】，格式如：99.00:");
                } else if (5 == step) {
                    try {
                        Double price = Double.valueOf(in);
                        product.setPrice(new BigDecimal(price));
                        action.insertProduct(product);
                        System.out.println("新增商品成功！");
                        step = 0;
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        System.out.println("新增商品失败！");
                        step = 0;
                    }
                }
            }

            if (OPERATION_UPDATE.equals(in.toUpperCase()) || OPERATION_UPDATE.substring(0, 1).equals(in.toUpperCase())
                    || OPERATION_UPDATE.equals(preOpt)) {
                preOpt = OPERATION_UPDATE;
                // 新增商品
                if (1 == step) {
                    System.out.print("请输入修改商品【ID】:");

                } else if (2 == step) {
                    Product p = action.getInfo(Integer.valueOf(in));
                    if (p == null) {
                        System.out.println("该商品不存在！");
                    } else {
                        product.setId(Integer.valueOf(in));
                        System.out.print("请输入商品【名称】:");
                    }
                } else if (3 == step) {
                    product.setName(in);
                    System.out.print("请输入商品【类型】:");
                } else if (4 == step) {
                    product.setType(in);
                    System.out.print("请输入商品【价格】，格式如：99.00:");
                } else if (5 == step) {
                    try {
                        Double price = Double.valueOf(in);
                        product.setPrice(new BigDecimal(price));
                        int count = action.updateProduct(product);
                        if (count > 0) {
                            System.out.println("修改商品成功！");
                        } else {
                            System.out.println("修改商品失败！");
                        }
                        step = 1;
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        System.out.println("修改商品失败！");
                        step = 1;
                    }
                }
            }

            if (OPERATION_ADD.equals(preOpt) || OPERATION_UPDATE.equals(preOpt)) {
                step++;
            }

            //退出当前功能，返回主菜单
            if (OPERATION_BREAK.equals(in.toUpperCase())//退出当前功能，返回主菜单
                    || OPERATION_BREAK.substring(0, 1).equals(in.toUpperCase())) {
                step = 1;      //跳转到步骤1
                preOpt = null; //删除所保留的记忆
                product = null; //将product中的值清空
                System.out.println("\n" + CONTEXT);
            }
        }
    }

}